package com.gluck.cases.boostrap.controller;

import com.github.pagehelper.PageInfo;
import com.gluck.cases.app.biz.impl.CasesApiService;
import com.gluck.cases.app.biz.impl.CasesGroupService;
import com.gluck.cases.app.biz.impl.CasesTestsService;
import com.gluck.cases.common.utils.enums.BizSerErrorEnum;
import com.gluck.cases.common.utils.utils.AssertUtil;
import com.gluck.cases.common.utils.utils.BizResult;
import com.gluck.cases.core.modal.assemble.CasesApiAssemble;
import com.gluck.cases.core.modal.assemble.CasesGroupAssemble;
import com.gluck.cases.core.modal.dto.CasesApiDTO;
import com.gluck.cases.core.modal.dto.CasesGroupDTO;
import com.gluck.cases.core.modal.req.AssociatedCasesTestsByGidReq;
import com.gluck.cases.core.modal.req.CasesApiReq;
import com.gluck.cases.core.modal.req.CasesGroupReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @ClassName CasesController
 * @Desc CasesApiController controller
 * @Author shiyongzhe
 * @Date 2022/6/3 - 7:53 下午
 **/
@RestController
@RequestMapping("cases")
public class CasesController {

    @Autowired
    private CasesApiService casesApiService;

    @Autowired
    private CasesGroupService casesGroupService;

    @Autowired
    private CasesTestsService casesTestsService;

    /**
     * 功能描述: 查询搜索测试用例接口列表
     * @Params [req]
     * @Return com.gluck.cases.common.utils.utils.BizResult
     * @Author I`m Gluck
     * @Date 2022/6/3
     */
    @RequestMapping(value = "/query_cases_api", method = RequestMethod.GET)
    public BizResult query(@RequestBody CasesApiReq req){
        PageInfo<CasesApiDTO> result = casesApiService.queryList(CasesApiAssemble.ReqConvertCaseApiDTO(req), req.getPage(), req.getSize());
        return BizResult.newSuccessResult(result);
    }

    /**
     * 功能描述: 查询用例详情
     * @Params [id]
     * @Return com.gluck.cases.common.utils.utils.BizResult
     * @Author I`m Gluck
     * @Date 2022/6/3
     */
    @RequestMapping(value = "/query_cases_detail", method = RequestMethod.GET)
    public BizResult detail(@RequestParam("id") Long id){
        CasesApiDTO dto = casesApiService.queryDetailById(id);
        return BizResult.newSuccessResult(dto);
    }

    /**
     * 功能描述: 删除单个or 删除多个
     * @Params [ids]
     * @Return com.gluck.cases.common.utils.utils.BizResult
     * @Author I`m Gluck
     * @Date 2022/6/3
     */
    @RequestMapping(value = "/delete_cases_api", method = RequestMethod.POST)
    public BizResult delete(@RequestBody List<Long> ids){
        boolean deleteCasesApis = casesApiService.deleteCasesApis(ids);
        return BizResult.newSuccessResult(deleteCasesApis);
    }

    /**
     * 功能描述: 更新用例详情
     * @Params [req]
     * @Return com.gluck.cases.common.utils.utils.BizResult
     * @Author I`m Gluck
     * @Date 2022/6/3
     */
    @RequestMapping(value = "/update_cases_api", method = RequestMethod.POST)
    public BizResult update(@RequestBody CasesApiReq req){
        boolean updateCasesApi = casesApiService.updateCasesApi(CasesApiAssemble.ReqConvertCaseApiDTO(req));
        return BizResult.newSuccessResult(updateCasesApi);
    }

    /**
     * 功能描述: 查询搜索用例分组列表
     * @Params [req]
     * @Return com.gluck.cases.common.utils.utils.BizResult
     * @Author I`m Gluck
     * @Date 2022/6/3
     */
    @RequestMapping(value = "/query_cases_groups", method = RequestMethod.GET)
    public BizResult queryGroups(@RequestBody CasesGroupReq req){
        PageInfo<CasesGroupDTO> dtoPageInfo = casesGroupService.queryList(CasesGroupAssemble.CasesGroupReqConvertDTO(req), req.getPage(), req.getSize());
        return BizResult.newSuccessResult(dtoPageInfo);
    }

    /**
     * 功能描述: 更新用例分组信息
     * @Params [req]
     * @Return com.gluck.cases.common.utils.utils.BizResult
     * @Author I`m Gluck
     * @Date 2022/6/3
     */
    @RequestMapping(value = "/update_cases_group", method = RequestMethod.POST)
    public BizResult updateGroups(@RequestBody CasesGroupReq req){
        boolean update = casesGroupService.updateCasesApi(CasesGroupAssemble.CasesGroupReqConvertDTO(req));
        return BizResult.newSuccessResult(update);
    }

    @RequestMapping(value = "/associated_cases_tests", method = RequestMethod.POST)
    public BizResult associatedCasesTests(@RequestBody AssociatedCasesTestsByGidReq req){
        // 校验请求参数
        AssertUtil.isTrue(Objects.isNull(req.getGroupId()) || Objects.isNull(req.getGroupName()), BizSerErrorEnum.REQUEST_PARAMS_ERROR);
        boolean result = casesGroupService.associateCasesTests(req.getGroupId(), req.getGroupName(), req.getCasesApis());
        return BizResult.newSuccessResult(result);
    }



}
